//
//  XZHHomeTableView.m
//  Piece
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHHomeTableView.h"
#import "XZHHomeTableViewCell.h"
#import "XZHHomeCollectionViewCell.h"
#import "XZHHomeModel.h"
#import "Masonry.h"
#import "XZHSearchViewController.h"

@implementation XZHHomeTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self addSubview:view];
        self.backgroundColor = [UIColor clearColor];
        UIButton *btn_note = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn_note.backgroundColor = [UIColor redColor];
        btn_note.frame = CGRectMake(5, ScreenHeight - 62 -49 - 64, 60, 40);
//        [btn_note makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.left).offset(10);
//            make.bottom.equalTo(self.bottom).offset(-30);
//            make.height.equalTo(20);
//            make.width.equalTo(50);
//        }];
        [btn_note addTarget:self action:@selector(didClickedNoteButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn_note];
        [btn_note setTag:1212];
        
        
        UIButton *btn_more = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn_more.backgroundColor = [UIColor greenColor];
        [self addSubview:btn_more];
        [btn_more makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.right).offset(-8);
            make.top.equalTo(btn_note).offset(-5);
            make.width.equalTo(48);
            make.height.equalTo(45);
        }];
        [btn_more setTag:1211];
        [btn_more addTarget:self action:@selector(didClickedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *btn_heart = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn_heart];
        [btn_heart makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(btn_more.left).offset(-30);
            make.top.equalTo(btn_more).offset(1);
            make.width.equalTo(48);
            make.height.equalTo(45);
        }];
        [btn_heart setTag:1210];
        [btn_heart addTarget:nil action:@selector(didClickedLikeButton:) forControlEvents:UIControlEventTouchUpInside];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 49 - 64) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        [self.tableView registerNib:[UINib nibWithNibName:@"XZHHomeTableViewCell" bundle:nil] forCellReuseIdentifier:@"homeTableViewCell"];
        
    }
    return self;
}
- (void)didClickedLikeButton:(UIButton *)sender {
    [self.delegate changLikeImageView:self.model];
    
}

- (void)didClickedMoreButton:(UIButton *)sender {
    [self.delegate shareHP:self.model];
}

- (void)didClickedNoteButton:(UIButton *)sender {
    [self.delegate editNote:self.model];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [tableView setContentOffset:CGPointMake(0, 0) animated:NO];
        XZHHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homeTableViewCell"];
        cell.model = self.model;
        [self insertSubview:[self viewWithTag:1210] belowSubview:cell];
        [self insertSubview:[self viewWithTag:1211] belowSubview:cell];
        [self insertSubview:[self viewWithTag:1212] belowSubview:cell];
        cell.homeVC = _homeVC;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.backgroundColor = [UIColor clearColor];
        cell.userInteractionEnabled = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        XZHHomeModel *model = self.model;
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName,nil];
        CGRect newRect = [model.hp_content boundingRectWithSize:CGSizeMake(ScreenWidth - 36, 8000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
        return 420 + newRect.size.height + 75;
    } else {
        return 70;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.tableView.contentOffset.y < -120) {
        [self.delegate swipeTableView];
    }
}

-(void)setModel:(XZHHomeModel *)model {
    if (_model != model) {
        _model = model;
        [self.tableView reloadData];
    }
}

@end
